package com.agrm.member.service.impl;

import com.agrm.member.mapper.MemberMapper;
import com.agrm.member.service.MemberService;
import com.agrm.model.brand.pojo.Brand;
import com.agrm.model.common.dtos.PageRequestDto;
import com.agrm.model.member.dto.MemberQuery;
import com.agrm.model.member.pojo.Member;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@CacheConfig(cacheNames = "stmall-seckills-member")
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private MemberMapper memberMapper;


    /**
     * 条件分页查询
     * @param page  分页参数
     * @param query 查询参数
     * @return
     */
    @Override
    public IPage<Member> pageQueryList(PageRequestDto page, MemberQuery query) {
        page.checkParam();
        // 创建MyBatis-Plus分页对象
        Page<Member> pages = new Page<>(page.getPage(), page.getSize());
        // 构建查询条件
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        if (query != null){
//        private String name; //姓名 模糊查询
            if (query.getName() != null && !query.getName().isEmpty()) {
                wrapper.like(Member::getName, query.getName());
            }
//        private String sex; // 性别
            if (query.getSex() != null && !query.getSex().isEmpty()) {
                wrapper.eq(Member::getSex, query.getSex());
            }
//        private LocalDate startBirthDate; // 出生开始日期
//        private LocalDate endBirthDate; // 出生结束日期  范围查询
            if (query.getStartBirthDate() != null && query.getEndBirthDate() != null) {
                wrapper.between(Member::getBirthday, query.getStartBirthDate(), query.getEndBirthDate());
            }
//        private String phone; // 手机号 模糊查询
            if (query.getPhone() != null && !query.getPhone().isEmpty()) {
                wrapper.like(Member::getPhone, query.getPhone());
            }
//        private String wedlock;//婚姻状态
            if (query.getWedlock() != null && !query.getWedlock().isEmpty()) {
                wrapper.eq(Member::getWedlock, query.getWedlock());
            }
        }

        return memberMapper.selectPage(pages, wrapper);
    }

    @Cacheable(key = "#id")
    @Override
    public Member getById(Integer id) {
        return memberMapper.selectById(id);
    }

}
